package com.ruoyi.equipment.controller;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.file.FileUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.equipment.domain.EquipmentDetectionReport;
import com.ruoyi.equipment.service.IEquipmentDetectionReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2022-12-14
 */
@RestController
@RequestMapping("/equipment/report")
public class EquipmentDetectionReportController extends BaseController
{
    @Autowired
    private IEquipmentDetectionReportService equipmentDetectionReportService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(EquipmentDetectionReport equipmentDetectionReport)
    {
        startPage();
        List<EquipmentDetectionReport> list = equipmentDetectionReportService.selectEquipmentDetectionReportList(equipmentDetectionReport);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:report:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipmentDetectionReport equipmentDetectionReport)
    {
        List<EquipmentDetectionReport> list = equipmentDetectionReportService.selectEquipmentDetectionReportList(equipmentDetectionReport);
        ExcelUtil<EquipmentDetectionReport> util = new ExcelUtil<EquipmentDetectionReport>(EquipmentDetectionReport.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(equipmentDetectionReportService.selectEquipmentDetectionReportById(id));
    }


    @GetMapping(value = "downloadFile")
    public void downloadFile( Long id,HttpServletResponse response) throws IOException {
        final EquipmentDetectionReport equipmentDetectionReport = equipmentDetectionReportService.selectEquipmentDetectionReportById(id);
        final String detectionReportFilePath = equipmentDetectionReport.getDetectionReportFilePath();
        response.setContentType("application/octet-stream");
        final ServletOutputStream outputStream = response.getOutputStream();
        StreamUtils.copy(Files.newInputStream(Paths.get(detectionReportFilePath)),outputStream);


    }
    @PreAuthorize("@ss.hasPermi('equipment:report:query')")
    @GetMapping(value = "getFileName")
    public AjaxResult getFileName( Long id) throws IOException {
        final EquipmentDetectionReport equipmentDetectionReport = equipmentDetectionReportService.selectEquipmentDetectionReportById(id);
        return AjaxResult.success("",equipmentDetectionReport.getDetectionReportFileName());

    }
    String basePath = "./";
    private Path getPath(EquipmentDetectionReport equipmentDetectionReport){

       return Paths.get(basePath,  equipmentDetectionReport.getDetectionCertCode()+"-"+
                equipmentDetectionReport.getReportFile().getOriginalFilename());
    }
    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('equipment:report:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(EquipmentDetectionReport equipmentDetectionReport) throws IOException {
        final MultipartFile reportFile = equipmentDetectionReport.getReportFile();
        final Path path = getPath(equipmentDetectionReport);
        reportFile.transferTo(path);
        equipmentDetectionReport.setDetectionReportFilePath(path.toString());
        equipmentDetectionReport.setDetectionReportFileName(reportFile.getOriginalFilename());
        return toAjax(equipmentDetectionReportService.insertEquipmentDetectionReport(equipmentDetectionReport));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('equipment:report:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentDetectionReport equipmentDetectionReport)
    {
        return toAjax(equipmentDetectionReportService.updateEquipmentDetectionReport(equipmentDetectionReport));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('equipment:report:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(equipmentDetectionReportService.deleteEquipmentDetectionReportByIds(ids));
    }
}
